﻿/*
 * Created by SharpDevelop.
 * User: Masy Freddy
 * Date: 20/09/2011
 * Time: 12:43
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.AddIn.Hosting;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;

using MetaVideoEditorEvolved.AddIn.HostViews;
using MetaVideoEditorEvolved.Engine.Entity;

namespace MetaVideoEditorEvolved.Engine
{
	/// <summary>
	/// Description of AddInSaverManager.
	/// </summary>
	public class AddInSaverManager : AddInManager
	{
		private static AddInSaverManager currentInstance;
		private List<KeyValuePair<AddInInformation, SaverView>> saverAddins = new List<KeyValuePair<AddInInformation, SaverView>>();

		public AddInSaverManager()
		{
			Collection<AddInToken> saverAddIns = AddInStore.FindAddIns(typeof(AddIn.HostViews.SaverView), addInPath);
			
			InsertAddIns(saverAddIns, AddInType.Saver);
			
			InitializeAddIn();
		}
		
		public void InitializeAddIn()
		{
			foreach (var addin in AddIns) {
				if(addin.IsEnable &&
				   !saverAddins.Where((o) => o.Key == addin).Any())
				{
					SaverView addInInstance = addin.Token.Activate<SaverView>(AddInSecurityLevel.FullTrust);
					saverAddins.Add(new KeyValuePair<AddInInformation, SaverView>(addin, addInInstance));
				}
			}
		}
		
		public static AddInSaverManager Current
		{
			get
			{
				if(currentInstance == null)
				{
					currentInstance = new AddInSaverManager();
				}
				
				return currentInstance;
			}
		}
		
		public void SaveMovie(string mediaLocation, Movie movie)
		{
			if(!movie.IsInError)
			{
				foreach (var element in saverAddins) {
					SaverView addInInstance = element.Value;
					
					addInInstance.SaveMovie(mediaLocation, new AddIn.Entity.Movie()
					                        {
					                        	Title = movie.Title,
					                        	OriginalTitle = movie.OriginalTitle,
					                        	Trailers = new List<MetaVideoEditorEvolved.AddIn.Entity.Trailer>(from t in  movie.Trailers
					                        	                                                                 select new AddIn.Entity.Trailer
					                        	                                                                 {
					                        	                                                                 	HighQuality = t.HighQuality,
					                        	                                                                 	MediumQuality = t.MediumQuality,
					                        	                                                                 	LowQuality = t.LowQuality,
					                        	                                                                 	Language =(AddIn.Entity.Language) Enum.Parse(typeof(AddIn.Entity.Language), t.Language.ToString()) ,
					                        	                                                                 	DefaultPath = t.DefaultPath
					                        	                                                                 }),
					                        	Backdrops = movie.Backdrops.ToList(),
					                        	Countries = movie.Nationalities.ToList(),
					                        	Genres = movie.Genres.ToList(),
					                        	Studios = movie.Studios.ToList(),
					                        	CastMembers = new List<MetaVideoEditorEvolved.AddIn.Entity.CastMember>(from c in movie.CastMembers
					                        	                                                                       select new MetaVideoEditorEvolved.AddIn.Entity.CastMember
					                        	                                                                       {
					                        	                                                                       	Activity = c.Activity,
					                        	                                                                       	FullName = c.FullName,
					                        	                                                                       	Picture = c.Picture,
					                        	                                                                       	Role = c.Role
					                        	                                                                       }),
					                        	Duration = movie.Duration,
					                        	Overview = movie.Overview,
					                        	Posters = movie.Posters.ToList(),
					                        	Rating = movie.Rating,
					                        	Year = movie.Year,
					                        	FullPath = movie.FullPath
					                        });
				}
			}
		}
		
		public void SaveEpisode(string mediaLocation, Episode episode)
		{
			if(!episode.IsInError)
			{
				foreach (var element in saverAddins) {
					SaverView addInInstance = element.Value;
					
					addInInstance.SaveEpisode(mediaLocation, new AddIn.Entity.Episode()
					                          {
					                          	Images = new List<Uri>( episode.Images),
					                          	Name = episode.Name,
					                          	Id = episode.Id,
					                          	Number = episode.Number,
					                          	Overview = episode.Overview,
					                          	Rating = episode.Rating,
					                          	Year = episode.Year,
					                          	FirstAired = episode.FirstAired,
					                          	Directors =new List<string>(episode.Directors),
					                          	GuestStars = new List<string>(episode.GuestStars),
					                          	Writers = new List<string>(episode.Writers),
					                          	SeasonNumber = episode.SeasonNumber
					                          });
				}
			}
		}
		
		public void SaveSeason(string mediaLocation, Season season)
		{
			if(!season.IsInError)
			{
				foreach (var element in saverAddins) {
					SaverView addInInstance = element.Value;
					
					addInInstance.SaveSeason(mediaLocation, new AddIn.Entity.Season
					                         {
					                         	Images = new List<Uri>(season.Images),
					                         	Number = season.Number
					                         });
				}
			}
		}
		
		public void SaveSerie(string mediaLocation, Serie serie)
		{
			if(!serie.IsInError)
			{
				foreach (var element in saverAddins) {
					SaverView addInInstance = element.Value;
					
					addInInstance.SaveSerie(mediaLocation, new AddIn.Entity.Serie()
					                        {
					                        	Name = serie.Name,
					                        	Overview = serie.Overview,
					                        	Backdrops =new List<Uri>(serie.Backdrops),
					                        	CastMembers = new List<MetaVideoEditorEvolved.AddIn.Entity.CastMember>(from c in serie.CastMembers
					                        	                                                                       select new MetaVideoEditorEvolved.AddIn.Entity.CastMember
					                        	                                                                       {
					                        	                                                                       	Activity = c.Activity,
					                        	                                                                       	FullName = c.FullName,
					                        	                                                                       	Picture = c.Picture,
					                        	                                                                       	Role = c.Role
					                        	                                                                       }),
					                        	Genres = new List<string>(serie.Genres),
					                        	Graphicals = new List<Uri>(serie.Graphicals),
					                        	Posters = new List<Uri>(serie.Posters),
					                        	Studios = new List<string>(serie.Studios),
					                        	Duration = serie.Duration,
					                        	Id = serie.Id,
					                        	Rating = serie.Rating,
					                        	Year = serie.Year
					                        });
				}
			}
		}
		
		public void ShowOption(AddInInformation addInInformation)
		{
			AddInProcess addInProcess = new AddInProcess();
			addInProcess.Start();

			SaverView addInInstance =  addInInformation.Token.Activate<SaverView>(addInProcess, AddInSecurityLevel.FullTrust);
			
			addInInstance.ShowOption();
			
			addInProcess.Shutdown();
		}
		
		public bool HasOption(AddInInformation addInInformation)
		{
			var addin = saverAddins.Where((o) => o.Key == addInInformation);
			
			SaverView addInInstance = null;
			
			if(addin.Any())
			{
				addInInstance = addin.First().Value;
			}
			else
			{
				addInInstance = addInInformation.Token.Activate<SaverView>(AddInSecurityLevel.FullTrust);
			}
			
			return addInInstance.HasOption();
		}
		
		public override void SaveAllConfiguration()
		{
			base.SaveAllConfiguration();
			
			saverAddins = new List<KeyValuePair<AddInInformation, SaverView>>();
			
			InitializeAddIn();
		}
	}
}
